#pragma once

#include <optional>
#include <string>
#include "http-status-code.h"
#include "http-content-type.h"
#include "http-response-buffer.h"


class HttpResponse
{
public:
    HttpResponse() : contentType_("text", "html") {}

    HttpStatusCode statusCode() const {
        return statusCode_;
    }

    void setStatusCode(HttpStatusCode statusCode) {
        statusCode_ = statusCode;
    }

    const http::ContentType &contentType() const {
        return contentType_;
    }

    void setContentType(const http::ContentType &contentType) {
        contentType_ = contentType;
    }

    HttpResponseBufferHolder takeBuffer() {
        HttpResponseBufferHolder holder(std::move(bufferHolder_));
        return holder;
    }

    std::string &buffer() {
        return bufferHolder_.as<OwnMemoryHttpResponseBuffer>().buffer();
    }

    const std::string *etag() const {
        if (etag_) {
            return &(etag_.value());
        } else {
            return nullptr;
        }
    }

    void setEtag(const std::string &etag) {
        etag_ = etag;
    }

    bool isGzipped() const {
        return gzipped_;
    }

    void markGzipped() {
        gzipped_ = true;
    }

    // 丢弃，直接关闭连接，不返回数据
    void discard() {
        discarded_ = true;
    }

    // 若被标记为丢弃，则不返回数据，直接关闭连接
    bool isDiscarded() const {
        return discarded_;
    }

private:
    HttpStatusCode statusCode_ = HttpStatusCode::Ok;
    http::ContentType contentType_;
    HttpResponseBufferHolder bufferHolder_;
    std::optional<std::string> etag_;
    bool gzipped_ = false;

    // 如果设置了丢弃标记，则不会给客户端返回有效内容，直接仅关闭连接
    // 这个标记用于收到了恶意请求的情况
    bool discarded_ = false;
};
